﻿using System;
using KinonekoSoftware.Foundation.UI.Media;

namespace KinonekoSoftware.Studio.Pages.Theories.Elements
{

#if WINDOWS


    using System.Windows.Media.Imaging;
    using KinonekoSoftware.CodariaDB.Documents.Theories.Elements;
    using KinonekoSoftware.CodariaDB.Utils;

    [Operation(ResourceType.Icon)]
    public partial class NewElementViewModel
    {
        private ImageEditFeedback _imageIEF;
        private ImageEditFeedback _iconIEF;
        private BitmapImage       _icon;
        private BitmapImage       _image;
        private Color             _foregroundColor;
        private Color             _backgroundColor;
        
        private static void GetBitmap(string value, Action<BitmapImage> callback)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            if (!Platform.Cache.TryGetValue(value, out var wIMG))
            {
                return;
            }

            if (!wIMG.TryGetTarget(out var img) && img is BitmapImage)
            {
                return;
            }
            
            callback?.Invoke((BitmapImage)img);
        }

        private async void DoSelectIconCommand()
        {
            var r = await Platform.Icon();

            if (!r.IsFinished)
            {
                if (!string.IsNullOrEmpty(r.Reason))
                {
                    await Danger(TitleSR.DangerOperation, r.Reason);
                }
                return;
            }

            _iconIEF = r.Value;
            Icon     = _iconIEF.Image;
        }

        private async void DoSelectImageCommand()
        {
            var r = await Platform.Icon();

            if (!r.IsFinished)
            {
                if (!string.IsNullOrEmpty(r.Reason))
                {
                    await Danger(TitleSR.DangerOperation, r.Reason);
                }

                return;
            }

            _imageIEF = r.Value;
            Image     = _imageIEF.Image;
        }

        private void DoResetIconCommand()
        {
            Icon     = null;
            _iconIEF = null;

            if (_element is not null)
            {
                _element.Icon = null;
            }
        }

        private void DoResetImageCommand()
        {
            Image     = null;
            _imageIEF = null;

            if (_element is not null)
            {
                _element.Image = null;
            }
        }

        protected override async Task<Element> OnFinish(bool editing)
        {
            var iconFS = Platform.GetEngine<IconSystem>();
            
            if (editing)
            {

                if (_iconIEF is not null)
                {
                    var oldIcon = _element.Icon;
                    var newIcon = _iconIEF.ImageID;
                    
                    _element.Icon = newIcon;
                    
                    //
                    // 写入
                    await iconFS.WriteAsync(newIcon, _iconIEF.ImageBuffer);
                    
                    //
                    // 移除占用
                    iconFS.RemoveOccupation(oldIcon);
                    iconFS.InsertOccupation(newIcon);
                }

                if (_imageIEF is not null)
                {
                    
                    var oldImage = _element.Image;
                    var newImage = _imageIEF.ImageID;
                    
                    _element.Image = newImage;
                    
                    
                    await iconFS.WriteAsync(newImage, _imageIEF.ImageBuffer);
                    
                    
                    //
                    // 移除占用
                    iconFS.RemoveOccupation(oldImage);
                    iconFS.InsertOccupation(newImage);
                }

                _element.Background = _background;
                _element.Foreground = _foreground;
                _element.Name       = _name;
                _element.Intro      = _intro;
            }
            else
            {
                if (_iconIEF is not null)
                {
                    var newIcon = _iconIEF.ImageID;
                    
                    //
                    // 写入
                    await iconFS.WriteAsync(newIcon, _iconIEF.ImageBuffer);
                    
                    
                    iconFS.InsertOccupation(newIcon);
                }

                if (_imageIEF is not null)
                {
                    var newImage = _imageIEF.ImageID;
                    
                    await iconFS.WriteAsync(newImage, _imageIEF.ImageBuffer);
                    
                    iconFS.InsertOccupation(newImage);
                }

                _element = new Element
                {
                    Id         = ID.Get(),
                    Background = _background,
                    Foreground = _foreground,
                    Icon       = _iconIEF?.ImageID,
                    Image      = _imageIEF?.ImageID,
                    Name       = _name,
                    Intro      = _intro,
                };
            }

            return _element;
        }


        public Color BackgroundColor
        {
            get => _backgroundColor;
            set
            {
                SetValue(ref _backgroundColor, value);
                Background    = value.ToString();
                BackgroundRGB = $"A:{value.A}, R:{value.R}, G:{value.G}, B:{value.B}";
            }
        }

        public Color ForegroundColor
        {
            get => _foregroundColor;
            set
            {
                SetValue(ref _foregroundColor, value);
                Foreground    = value.ToString();
                ForegroundRGB = $"A:{value.A}, R:{value.R}, G:{value.G}, B:{value.B}";
            }
        }

        public BitmapImage Image
        {
            get => _image;
            set => SetValue(ref _image, value);
        }

        public BitmapImage Icon
        {
            get => _icon;
            set => SetValue(ref _icon, value);
        }


        public string ForegroundRGB
        {
            get => _foregroundRGB;
            private set => SetValue(ref _foregroundRGB, value);
        }
        public string BackgroundRGB
        {
            get => _backgroundRgb;
            private set => SetValue(ref _backgroundRgb, value);
        }
    }
#endif
}